package com.admin.service;

import com.admin.dao.ApplyDao;
import com.admin.entity.Apply;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

//预约信息接口类
@Service
public class ApplyService extends BaseService {

    @Autowired
    private ApplyDao applyDao;

    //【预约信息】设置查询条件
    private LambdaQueryWrapper<Apply> getApplyQueryCondition(Apply apply) {
        LambdaQueryWrapper<Apply> lambdaQuery = this.getBaseQueryCondition(apply);
        //根据创建时间排序
        lambdaQuery.orderByDesc(Apply::getCreateDate);
        if (ObjectUtils.isNotNull(apply)) {
            //【预约号】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(apply.getApplyCode()), Apply::getApplyCode, apply.getApplyCode());
            //【预约人】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(apply.getRegisterId()), Apply::getRegisterId, apply.getRegisterId());
            //【预约科室】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(apply.getOfficeId()), Apply::getOfficeId, apply.getOfficeId());
            //【医生姓名】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(apply.getUserId()), Apply::getUserId, apply.getUserId());
            //【预约日期】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(apply.getApplyDate()), Apply::getApplyDate, apply.getApplyDate());
            //【预约日期】范围查询
            if (ObjectUtils.isNotNull(apply.getApplyDateRange()) && apply.getApplyDateRange().size() > 0) {
                lambdaQuery.between(Apply::getApplyDate, apply.getApplyDateRange().get(0), apply.getApplyDateRange().get(1));
            }
            //【预约时间】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(apply.getApplyTime()), Apply::getApplyTime, apply.getApplyTime());
            //【预约时间】范围查询
            if (ObjectUtils.isNotNull(apply.getApplyTimeRange()) && apply.getApplyTimeRange().size() > 0) {
                lambdaQuery.between(Apply::getApplyTime, apply.getApplyTimeRange().get(0), apply.getApplyTimeRange().get(1));
            }
            //【预约状态】范围查询
            lambdaQuery.in(ObjectUtils.isNotNull(apply.getApplyStatusRange()), Apply::getApplyStatus, apply.getApplyStatusRange());
            //【预约状态】精确查询
            lambdaQuery.eq(ObjectUtils.isNotNull(apply.getApplyStatus()), Apply::getApplyStatus, apply.getApplyStatus());
        }
        return lambdaQuery;
    }

    //【预约信息】分页查询
    public IPage<Apply> getPage(Page<Apply> page, Apply apply) {
        LambdaQueryWrapper<Apply> lambdaQuery = getApplyQueryCondition(apply);
        return applyDao.selectPage(page, lambdaQuery);
    }
    
    //【预约信息】查询列表
    public List<Apply> getList(Apply apply) {
        LambdaQueryWrapper<Apply> lambdaQuery = getApplyQueryCondition(apply);
        return applyDao.selectList(lambdaQuery);
    }
    
    //【预约信息】根据id查询
    public Apply get(String id) {
        return applyDao.selectById(id);
    }

    //【预约信息】根据对象查询
    public Apply get(Apply apply) {
    LambdaQueryWrapper<Apply> lambdaQuery = getApplyQueryCondition(apply);
        return applyDao.selectOne(lambdaQuery);
    }
    
    //【预约信息】新增
    public int insert(Apply apply) {
        this.preInsert(apply);
        return applyDao.insert(apply);
    }
    
    //【预约信息】修改
    public int update(Apply apply) {
        this.preUpdate(apply);
        return applyDao.updateById(apply);
    }
    
    //【预约信息】删除
    public int delete(String id) {
        return applyDao.deleteById(id);
    }

    //【预约信息】批量删除
    public int delAll(String[] ids) {
        return applyDao.deleteBatchIds(Arrays.asList(ids));
    }

}